package hxy.scan.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import hxy.scan.server.dao.model.GoodsModel;
import hxy.scan.server.entity.BaseResponse;
import hxy.scan.server.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class GoodsController {

    @Resource
    GoodsService goodsService;

    /**
     * 增加
     * 如果有RequestBody就需要用json来请求这个接口（API）
     */
    @PostMapping("goods")
    public BaseResponse addGoods(@RequestBody GoodsModel goodsModel) {
//         插入之前应该查询是否存在，依据number查询
        QueryWrapper queryWrapper = new QueryWrapper();
//        equal
        queryWrapper.eq("number", goodsModel.getNumber());
        GoodsModel goodsModel2 = (GoodsModel) goodsModel.selectOne(queryWrapper);

        if (goodsModel2 == null) {
            boolean insert = goodsModel.insert();
            return BaseResponse.success(insert);
        } else {
            return BaseResponse.success(goodsModel);
        }

    }

    /**
     * 删除
     */
    @DeleteMapping("goods")
    public BaseResponse deleteGoods(@RequestBody GoodsModel goodsModel) {
        boolean insert = goodsModel.deleteById();
        return BaseResponse.success(insert);
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteGoodsByNumber")
    public BaseResponse deleteGoodsByNumber(@RequestBody GoodsModel goodsModel) {
        QueryWrapper<Object> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("number", goodsModel.getNumber());
        boolean insert = goodsModel.delete(objectQueryWrapper);
        return BaseResponse.success(insert);
    }


    /**
     * 修改
     */
    @PutMapping("goods")
    public BaseResponse updateGoods(@RequestBody GoodsModel goodsModel) {
        boolean insert = goodsModel.updateById();
        return BaseResponse.success(insert);
    }

    /**
     * 修改
     */
    @PutMapping("updateGoodsByNumber")
    public BaseResponse updateGoodsByNumber(@RequestBody GoodsModel goodsModel) {
        QueryWrapper<GoodsModel> objectUpdateWrapper = new QueryWrapper<>();
        //  依据number修改
        objectUpdateWrapper.eq("number", goodsModel.getNumber());
        boolean insert = goodsModel.update(objectUpdateWrapper);
        return BaseResponse.success(insert);
    }


    /**
     * 查询
     */
    @GetMapping("goods")
    public BaseResponse getGoods(GoodsModel goodsModel) {
        String id = goodsModel.getNumber();
        goodsModel = (GoodsModel) goodsModel.selectById();
        if (goodsModel == null) {
            log.warn("\n===>查询结果为空，number=[{}]不存在", id);
        }
        return BaseResponse.success(goodsModel);
    }

    /**
     * 查询
     */
    @GetMapping("goodsByNumber")
    public BaseResponse getGoodsByNumber(GoodsModel goodsModel) {
        QueryWrapper queryWrapper = new QueryWrapper();
//        equal
        queryWrapper.eq("number", goodsModel.getNumber());
        goodsModel = (GoodsModel) goodsModel.selectOne(queryWrapper);
        return BaseResponse.success(goodsModel);
    }


    /**
     * TODO 模糊查询
     * 难度有点大
     * https://baomidou.com/guide/wrapper.html#like
     */
    @GetMapping("goods/like")
    public BaseResponse likeGoods(GoodsModel goodsModel) {
        goodsModel = (GoodsModel) goodsModel.selectById();
        return BaseResponse.success(goodsModel);
    }


    /**
     * 查询所有
     */
    @GetMapping("goods/list")
    public BaseResponse listGoods(GoodsModel goodsModel) {
        List<GoodsModel> list = goodsModel.selectList(null);
        return BaseResponse.success(list);
    }


    /**
     * 查询某个作者的所有书
     */
    @GetMapping("goods/list/author")
    public BaseResponse listGoodsByAuthor(GoodsModel goodsModel) {
//         条件是author
//      查询封装 对象
        QueryWrapper queryWrapper = new QueryWrapper();
//        equal
        queryWrapper.eq("anthor", goodsModel.getAuthor());

        List<GoodsModel> list = goodsModel.selectList(queryWrapper);
        return BaseResponse.success(list);
    }

    /**
     * 批量添加
     */
    @PostMapping("goods/list")
    public BaseResponse listPostGoods(@RequestBody List<GoodsModel> goodsModels) {

//        goodsModels.forEach(goodsModel -> {
//
//        });
        // TODO 应该更具number和时间来更新
        return goodsService.saveOrUpdateBatchList(goodsModels);

    }


    /**
     * 依据number批量添加
     */
    @PostMapping("goods/list/number")
    public BaseResponse listPostGoodsbyNumber(@RequestBody List<GoodsModel> goodsModels) {

        // TODO 难度最大 应该更具number和时间来更新。 先遍历list,挨个查询，比较时间，保留最新时间的记录。

        for (int i = 0; i < goodsModels.size(); i++) {
            GoodsModel goodsModel = goodsModels.get(i);
        }
        //遍历list
        for (GoodsModel goodsModel : goodsModels) {

            // 依据number查询对象是否存在，，不存在就插入
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("number", goodsModel.getNumber());
            GoodsModel goodsModel2 = (GoodsModel) goodsModel.selectOne(queryWrapper);

            if (goodsModel2 == null) {
                boolean insert = goodsModel.insert();
            }

        }
        return BaseResponse.success();
    }


}
